package org.erp.spu.controller;


import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.SpuBrand;
import org.erp.spu.entity.bo.SpuBrandBo;
import org.erp.spu.entity.dto.SpuBrandDto;
import org.erp.spu.entity.vo.SpuBrandVo;

import org.erp.spu.service.SpuBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.erp.common.domain.Result;

import javax.annotation.Resource;

import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 品牌表(SpuBrand)表控制层
 *
 * @author dingding
 * @since 2025-11-05 11:22:08
 */
@RestController
@RequestMapping("brand")
public class SpuBrandController {
    /**
     * 服务对象
     */
    @Resource
    private SpuBrandService spuBrandService;

    /**
     * 分页查询所有数据
     *
     * @param spuBrandBo 查询实体
     * @return 所有数据
     */
    @GetMapping("/getListByPage")
    public Result<DataPageResp<SpuBrandVo>> selectAll(SpuBrandBo spuBrandBo) {
        SpuBrandDto spuBrandDto = new SpuBrandDto();
        BeanUtils.copyProperties(spuBrandBo, spuBrandDto);
        PageQueryModel<SpuBrandVo> queryModel = spuBrandService.getListByPage(spuBrandDto);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));
    }
     /**
     * 查询所有数据
     *
     * @param spuBrandBo 筛选条件
     * @return 所有数据
     */
    @GetMapping("getList")
    public Result<List<SpuBrand>> getList(SpuBrandBo spuBrandBo) {
        return Result.success(spuBrandService.getList(spuBrandBo));
    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<SpuBrandVo> getDetails(@PathVariable Long id) {
        return Result.success(spuBrandService.getDetails(id));
    }

    /**
     * 新增品牌数据
     *
     * @param spuBrandDto 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> insert(@RequestBody SpuBrandDto spuBrandDto) {

        boolean result = spuBrandService.addSpuBrandDto(spuBrandDto);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改数据
     *
     * @param spuBrandDto
     * @return 修改结果
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody SpuBrandDto spuBrandDto) {
        boolean result = spuBrandService.updateSpuBrandDto(spuBrandDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @PostMapping("/del")
    public Result<Boolean> delete(@RequestBody SpuBrandDto spuBrandDto) {
        boolean result = spuBrandService.removeById(spuBrandDto.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }

}

